#include "i2c_driver.h"
#include "../utility.h"

static XIic IicInstance;		  /* The instance of the IIC device */

XIic *IicInstancePtr = &IicInstance;

int IIC_axi_initialize(u16 DeviceId)
{
	int Status;
	XIic_Config *ConfigPtr;	/* Pointer to configuration data */

	/*
	 * Initialize the IIC driver so that it is ready to use.
	 */
	ConfigPtr = XIic_LookupConfig(DeviceId);
	if (ConfigPtr == NULL) {
		return XST_FAILURE;
	}

	Status = XIic_CfgInitialize(IicInstancePtr, ConfigPtr,
			ConfigPtr->BaseAddress);
	if (Status != XST_SUCCESS) {
		xil_printf("Initialization Failed\r\n");
		return XST_FAILURE;
	}

	Status = XIic_SelfTest(IicInstancePtr);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}
    
    // XIic_SetOptions(IicInstancePtr, XII_GENERAL_CALL_OPTION);
    // XIic_SetAddress(IicInstancePtr, XII_ADDR_TO_SEND_TYPE, SENSOR_7417_ADDRESS);

    // XIic_Start(IicInstancePtr);

    return Status;
}

int ReadData(u8 *BufferPtr, u16 ByteCount)
{
	int Status;
	u32 CntlReg_1 = XIic_ReadReg(IIC_BASE_ADDRESS, XIIC_SR_REG_OFFSET);
    int ByteCount_i;
    ByteCount_i = XIic_Recv(IIC_BASE_ADDRESS, SENSOR_7417_ADDRESS, BufferPtr, ByteCount, XIIC_STOP);
    if(ByteCount_i != ByteCount){
        return XST_FAILURE;
    }
	// Status = XIic_MasterRecv(IicInstancePtr, BufferPtr, ByteCount);
	// if (Status != XST_SUCCESS) {
	// 	return XST_FAILURE;
	// }
	u32 CntlReg = XIic_ReadReg(IIC_BASE_ADDRESS, XIIC_SR_REG_OFFSET);
	//XIIC_CR_NO_ACK_MASK;
	while ( XIic_IsIicBusy(&IicInstance) == TRUE ) {

	}

	return XST_SUCCESS;
}

int WriteData(u8 *BufferPtr, u16 ByteCount)
{
	int Status;
	u32 CntlReg_1 = XIic_ReadReg(IIC_BASE_ADDRESS, XIIC_SR_REG_OFFSET);
    int ByteCount_i;
    ByteCount_i = XIic_Send(IIC_BASE_ADDRESS, SENSOR_7417_ADDRESS, BufferPtr, ByteCount, XIIC_STOP);
    if(ByteCount_i != ByteCount){
        return XST_FAILURE;
    }
	// Status = XIic_MasterSend(IicInstancePtr, BufferPtr, ByteCount);
	// if (Status != XST_SUCCESS) {
	// 	return XST_FAILURE;
	// }

	u32 CntlReg = XIic_ReadReg(IIC_BASE_ADDRESS, XIIC_SR_REG_OFFSET);
	//XIIC_CR_NO_ACK_MASK;

	while ( XIic_IsIicBusy(IicInstancePtr) == TRUE ) {

	}

	return XST_SUCCESS;
}


double get_board_temper(){
    int ByteCount;
    double temper = 0;
    u8 temp_reg = 0x00;
    u8 conf_reg = 0x01;
    u8 TxMsgPtr[2] = {0x01,0x00};
    u8 RxMsgPtr[2] = {0xff,0xff};

    u16 address = XIic_GetAddress(&IicInstance, XII_ADDR_TO_SEND_TYPE);
    int status;
    status = WriteData(&temp_reg, 1);
    if(status != XST_SUCCESS){
        return 0;
    }

    gw_sleep(DELAY_WR);
    status = WriteData(TxMsgPtr, 2);
    if(status != XST_SUCCESS){
        return 0;
    }

    status = WriteData(&temp_reg, 1);
    if(status != XST_SUCCESS){
        return 0;
    }

    gw_sleep(DELAY_WR);
    status = ReadData(RxMsgPtr, 2);
    if(status != XST_SUCCESS){
        return 0;
    }


    u16 place0 = (u16)RxMsgPtr[0];
    u16 place1 = (u16)RxMsgPtr[1];

	uint16_t tmp = (place0 << 2) | (place1 >> 6);
	tmp = tmp & 0x3ff;
	int msb = tmp & 0x2ff;
	if(msb){ // Negative
		temper = (tmp & 0x1ff - 512) * 1.0 / 4;
	}else{
		temper = (tmp) * 1.0 / 4;
	}

    return temper;
}

double get_bb_voltage(){
    int ByteCount;
    double bb_voltage = 0;
    u8 temp_reg = 0x04;
    u8 conf_reg = 0x01;
    u8 TxMsgPtr[2] = {0x01,0x60};
    u8 RxMsgPtr[2] = {0xff,0xff};

    u16 address = XIic_GetAddress(&IicInstance, XII_ADDR_TO_SEND_TYPE);
    int status;
    status = WriteData(&temp_reg, 1);
    if(status != XST_SUCCESS){
        return 0;
    }

    gw_sleep(DELAY_WR);
    status = WriteData(TxMsgPtr, 2);
    if(status != XST_SUCCESS){
        return 0;
    }

    status = WriteData(&temp_reg, 1);
    if(status != XST_SUCCESS){
        return 0;
    }

    gw_sleep(DELAY_WR);
    status = ReadData(RxMsgPtr, 2);
    if(status != XST_SUCCESS){
        return 0;
    }

    u16 place0 = (u16)RxMsgPtr[0];
    u16 place1 = (u16)RxMsgPtr[1];

	uint16_t tmp = (place0 << 2) | (place1 >> 6);
	tmp = tmp & 0x3ff;
	bb_voltage = tmp * 1.0 / 1024 *2.5/0.091;

    return bb_voltage;
}

// BB电流
double get_bb_current(){
    int ByteCount;
    double bb_current = 0;
    u8 temp_reg = 0x04;
    u8 conf_reg = 0x01;
    u8 TxMsgPtr[2] = {0x01,0x20};
    u8 RxMsgPtr[2] = {0xff,0xff};

    u16 address = XIic_GetAddress(&IicInstance, XII_ADDR_TO_SEND_TYPE);
    int status;
    status = WriteData(&temp_reg, 1);
    if(status != XST_SUCCESS){
        return 0;
    }

    gw_sleep(DELAY_WR);
    status = WriteData(TxMsgPtr, 2);
    if(status != XST_SUCCESS){
        return 0;
    }

    status = WriteData(&temp_reg, 1);
    if(status != XST_SUCCESS){
        return 0;
    }

    gw_sleep(DELAY_WR);
    status = ReadData(RxMsgPtr, 2);
    if(status != XST_SUCCESS){
        return 0;
    }

    u16 place0 = (u16)RxMsgPtr[0];
    u16 place1 = (u16)RxMsgPtr[1];

	uint16_t tmp = (place0 << 2) | (place1 >> 6);
	tmp = tmp & 0x3ff;

	bb_current = tmp * 1.0 / 1024 *2.5/0.2;
    
    return bb_current;
}

